package com.fau.cm.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalKeyValue;
import com.fau.cm.service.IDictionaryManagerService;
import com.fau.cm.util.HttpRequestUtil;
import com.fau.cm.util.Page;
import com.fau.cm.util.ParamUtil;
import com.fau.cm.util.SystemConfig;
import com.fau.cm.util.ValidatorUtil;

@Service
@Transactional(readOnly = true)
public class DictionaryManagerService extends BaseService implements IDictionaryManagerService {
	
	@Resource
	private HttpRequestUtil httpRequestUtil;
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
	public void deleteDictionary(MedicalKeyValue keyVal) {
		if(keyVal.getId().indexOf(",") != -1){
			String[] ids = keyVal.getId().split(",");
			for(String id : ids){
				this.iBaseDao.updateHql("delete from MedicalKeyValue a where a.id=? ", id);
			}
		}else{
			this.iBaseDao.deleteObject(keyVal);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
	public boolean saveDictionary(MedicalAdmin admin, MedicalKeyValue keyVal) {
		Set<ConstraintViolation<Object>> errorInfo = ValidatorUtil.validateObj(keyVal);
		if(errorInfo.size()>0){
			return false;
		}else{
			keyVal.setHospitalId(admin.getEnterpriseId());
			keyVal.setSystemType(SystemConfig.CM_SYSTEM_TYPE);
			return this.iBaseDao.saveObject(keyVal);
		}
	}

	@Override
	public Page findDictionaryByPage(MedicalAdmin admin, MedicalKeyValue keyVal, Map<String, Object> map) {
		List<Object> objectList = new ArrayList<Object>();
		StringBuilder statusSql = new StringBuilder("from MedicalKeyValue m where 1=1 and m.hospitalId=? and m.systemType=? ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalKeyValue m where 1=1 and m.hospitalId=? and m.systemType=? ");
		objectList.add(admin.getEnterpriseId());
		objectList.add(SystemConfig.CM_SYSTEM_TYPE);
		if(StringUtils.isNotBlank(keyVal.getKeyName())){
			countSql.append("and m.keyName=? ");
			statusSql.append("and m.keyName=? ");
			objectList.add(keyVal.getKeyName());
			
		}
		if(StringUtils.isNotBlank(keyVal.getKeyValue())){
			countSql.append("and m.keyValue like ? ");
			statusSql.append("and m.keyValue like ? ");
			objectList.add("%"+keyVal.getKeyValue()+"%");
			
		}
		if(StringUtils.isNotBlank(keyVal.getTypeName())){
			countSql.append("and m.typeName=? ");
			statusSql.append("and m.typeName=? ");
			objectList.add(keyVal.getTypeName());
			
		}
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
		
		List<MedicalKeyValue> keyValList = new ArrayList<MedicalKeyValue>();
		for(Object obj : page.getResult()) {
			MedicalKeyValue bean = (MedicalKeyValue) obj;
			MedicalKeyValue chField = (MedicalKeyValue)this.iBaseDao.getValueByKey("0", bean.getTypeName());
			if(null != chField) {
				bean.setChFieldVal(chField.getKeyValue());
			}
			keyValList.add(bean);
		}
		page.setResult(keyValList);
		return page;
	}
	
	@Override
	public boolean verKeyName(MedicalAdmin admin, String KeyName){
		MedicalKeyValue mkv = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), KeyName);
		if(null == mkv){
			return true;
		} else {
			return false;
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
	public void editSave(MedicalKeyValue keyVal) {
		MedicalKeyValue kv = (MedicalKeyValue) this.iBaseDao.getObject(MedicalKeyValue.class, keyVal.getId());
		kv.setKeyName(keyVal.getKeyName());
		kv.setKeyValue(keyVal.getKeyValue());
		kv.setTypeName(keyVal.getTypeName());
		this.iBaseDao.saveObject(kv);
	}

	@Override
	public List<MedicalKeyValue> findKeyTypeList() {
		return this.iBaseDao.findValueByType("0",SystemConfig.CM_SYSTEM_TYPE,SystemConfig.CH_FIELD_TYPE);
	}

	@Override
	public List<MedicalKeyValue> findCmKeyValList(MedicalAdmin admin, String sysType, String keyType) {
		return this.iBaseDao.findValueByType(admin.getEnterpriseId(), sysType, keyType);
	}
	
	@Override
	public List<MedicalKeyValue> findOmKeyValList(MedicalAdmin admin, String sysType, String keyType) {
		return this.iBaseDao.findValueByType(admin.getEnterpriseId(), sysType, keyType);
	}
	
	@Override
	public List<MedicalKeyValue> findWmKeyValList(MedicalAdmin admin, String sysType, String keyType) {
		return this.iBaseDao.findValueByType(admin.getEnterpriseId(), sysType, keyType);
	}

	@Override
	public String getTaskSaveVal(MedicalAdmin admin) {
		MedicalKeyValue taskSaveVal = (MedicalKeyValue) this.iBaseDao.getObjectByHql("from MedicalKeyValue a where a.hospitalId=? and a.typeName=? ", admin.getEnterpriseId(), SystemConfig.TASK_SAVE_TYPE);
		return taskSaveVal.getKeyName();
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
	public void editTaskSaveVal(MedicalAdmin admin, String taskSaveVal) {
		this.iBaseDao.updateHql("update MedicalKeyValue a set a.keyName=? where a.hospitalId=? and a.typeName=? ", taskSaveVal, admin.getEnterpriseId(), SystemConfig.TASK_SAVE_TYPE);
	}

	@Override
	@Transactional(propagation = Propagation.NEVER)
	public void syncData(String type) {
		httpRequestUtil.syncDataConnect(type);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
	public void setServerIp(MedicalKeyValue keyVal) {
		this.iBaseDao.updateHql("update MedicalKeyValue a set a.keyValue=? where a.typeName=? and a.hospitalId=? ", keyVal.getKeyValue(), SystemConfig.CTI_SERVER_IP_TYPE, keyVal.getHospitalId());
	}

	@Override
	public MedicalKeyValue getServerIp(String eid) {
		MedicalKeyValue keyVal = (MedicalKeyValue) this.iBaseDao.getObjectByHql("from MedicalKeyValue a where a.typeName=? and a.hospitalId=? ", SystemConfig.CTI_SERVER_IP_TYPE, eid);
		return keyVal;
	}

}
